package org.dtrd.modules.ocr.controller;


import lombok.extern.slf4j.Slf4j;
import org.dtrd.modules.ocr.entity.bean.*;
import org.dtrd.modules.ocr.entity.po.DtrdEntRdOcrMapping;
import org.dtrd.modules.ocr.entity.po.DtrdEntRdOcrMappingTable;
import org.dtrd.modules.ocr.mapper.OcrUpdateMapper;
import org.dtrd.modules.ocr.service.IDtrdEntRdOcrMappingTableService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.ocr.service.IDtrdEntRdOcrMappingService;
import org.dtrd.common.api.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RestController;
import org.dtrd.base.BaseController;

import javax.validation.constraints.NotNull;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author sun
 * @since 2022-09-06
 */
@Slf4j
@RestController
@RequestMapping("/ocr/mapping")
public class DtrdEntRdOcrMappingController extends BaseController {

    @Autowired
    private IDtrdEntRdOcrMappingService ocrMappingService;
    @Autowired
    private IDtrdEntRdOcrMappingTableService ocrMappingTableService;
    @Autowired
    private OcrUpdateMapper updateMapper;

    @ApiOperation(value = "查询列表", response = OcrMappingDetail.class)
    @PostMapping("/search")
    @AutoLog(value="查询列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getOcrMappingList(@RequestBody @Validated OcrMappingListRequest request){
        List<OcrMappingDetail> list = ocrMappingService.getOcrMappingList(request);
        return Result.OK(list);
    }

    @ApiOperation(value = "更新或保存映射关系", response = OcrMappingDetail.class)
    @PostMapping("/save")
    @AutoLog(value="查询列表", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateOcrMapping(@RequestBody @Validated OcrMappingDetail ocrMapping){
        boolean save = ocrMappingService.saveOrUpdateOcrMappingByWord(ocrMapping);
        return save ? Result.OK() : Result.error("操作失败");
    }

    @ApiOperation(value = "可映射数据表", response = OcrMappingTableListInfo.class)
    @GetMapping("/table/list")
    @AutoLog(value="查询列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getCanOcrTableList(){
        List<OcrMappingTableListInfo> list = ocrMappingTableService.getOcrMappingTableList(null);
        return Result.OK(list);
    }

    @GetMapping("/table/fields")
    @AutoLog(value="查询表结构", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getCanOcrTableFieldList(@RequestParam Integer tableDataId){

        DtrdEntRdOcrMappingTable dtrdEntRdOcrMappingTable = ocrMappingTableService.getById(tableDataId);
        return dtrdEntRdOcrMappingTable == null ? Result.error("数据异常") : Result.OK(updateMapper.findFieldByTableName(dtrdEntRdOcrMappingTable.getTableName()));
    }
}
